﻿using Config = System.Configuration.ConfigurationManager;

namespace KlearSetup
{
    static class Program
    {
        static void Main()
        {
            var useDevFabric = bool.Parse(Config.AppSettings["UseDevFabric"]);

            var address = useDevFabric ? "http://127.0.0.1:10002" : "https://table.core.windows.net";
            var account = useDevFabric ? "devstoreaccount1" : Config.AppSettings["AccountName"];
            var accessKey = useDevFabric
                ? "Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw=="
                : Config.AppSettings["AccountSharedKey"];

            var database = Config.AppSettings["DatabaseName"]; // If you want test with Development Storage, change to database choosed for your DevStorage

            var ai = new Microsoft.Samples.ServiceHosting.StorageClient.StorageAccountInfo(
                new System.Uri(address), null, account, accessKey);

            var setupHandle = System.Activator.CreateInstanceFrom(
                Config.AppSettings["SetupAssemblyFileName"], Config.AppSettings["SetupClassName"]);
            if (setupHandle == null)
                throw new System.Exception("Setup assembly and class names required");
            var setup = (KlearStorage.Setup) setupHandle.Unwrap();

            var createTablesContext = Config.AppSettings["CreateTablesContext"];
            var fillTablesContext = Config.AppSettings["FillTablesContext"];

            if (useDevFabric)
            {
                System.Console.WriteLine("Local storage table creation");
                System.Console.WriteLine("Recreate database '" + database + "' (press the key 'Y') ?");
                System.Console.WriteLine("(Make sure that it isn't currently used by the Development Storage)");
                var key = System.Console.ReadKey(true);
                if (key.Key == System.ConsoleKey.Y)
                {
                    var contextPath = setup.GetType().Assembly.Location;
                    var proc = new System.Diagnostics.Process();
                    if (database.Length > 0)
                        proc.StartInfo.Arguments = "/database:" + database;
                    proc.StartInfo.Arguments += " /forceCreate " + contextPath;
                    proc.StartInfo.FileName = System.IO.Path.Combine(
                        System.Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFiles),
                        @"Windows Azure SDK\v1.0\bin\DevtableGen.exe");
                    proc.StartInfo.CreateNoWindow = false;
                    // proc.StartInfo.RedirectStandardOutput = true;
                    // proc.StartInfo.RedirectStandardError = true;
                    proc.StartInfo.UseShellExecute = false;

                    System.Console.WriteLine("Starting DevtableGen " + proc.StartInfo.Arguments);
                    proc.Start();
                    proc.WaitForExit();
                    System.Console.WriteLine();
                    System.Console.WriteLine("Start DevServer, make sure that the database '" + database + "' is selected, and press a key...");
                    System.Console.ReadKey();
                    System.Console.WriteLine();
                }
            }
            else
            {
                setup.DeleteAndCreateTables(ai, createTablesContext);
            }

            setup.FillTables(ai, fillTablesContext);

            System.Console.WriteLine("Done! Press a key...");
            System.Console.ReadKey();
        }
    }
}
